﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Threading;
using SilverPOP.Data.Process.Entity;
using SilverPOP.Data.Process.BLL;
using SilverPOP.Library.BusinessEntities;
using SilverPOP.Library.Helpers;
using SilverPOP.Data.Process.DAL;
using Helpers;
using Utilities;


namespace SilverPOP.Data.Process.BLL
{
    public class BaseBLL
    {

        public static List<ContactListCreationEntity> PopulateContactEntityCreationObjects(long campaignID)
        {
            List<ContactListCreationEntity> contactEntityCreationList = new List<ContactListCreationEntity>();

            List<CampaignWithBlast> campaignWithBlast = BaseBLL.GetActiveCampaignWithItsBlast(campaignID);

            foreach (CampaignWithBlast campaignBlast in campaignWithBlast)
            {
                List<Segment> segmentList = GetSegmentListByCampaignID(campaignBlast.Campaign.CampaignID);
                ContactListCreationEntity contactEntity = new ContactListCreationEntity();

                contactEntity.CampaignAndItsBlast = campaignBlast;
                contactEntity.Segment = segmentList != null ? segmentList : null;

                List<object> silverPOPDatabaseIdAndBlastId = GetSilverPOPDatabaseIdByCampaignID(campaignBlast.Campaign.CampaignID);

                if (silverPOPDatabaseIdAndBlastId != null)
                {
                    contactEntity.SilverPOPDatabaseID = (long)silverPOPDatabaseIdAndBlastId[0];
                    contactEntity.SilverPOPDatabaseName = silverPOPDatabaseIdAndBlastId[1].ToString();
                }
                else
                {
                    contactEntity.SilverPOPDatabaseID = 0;
                    contactEntity.SilverPOPDatabaseName = String.Empty;
                }

                contactEntityCreationList.Add(contactEntity);
            }

            return contactEntityCreationList;
        }

        public static List<Segment> GetSegmentListByCampaignID(long campaignID)
        {

            List<Segment> segmentList = new List<Segment>();
            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@campaignId", campaignID));
            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Get_Segment_List_By_Campaign_Id", spParamValue);

            if (dataList != null)
            {

                foreach (DataReaderObject dataReader in dataList)
                {
                    Segment segment = new Segment();
                    segment.SegmentCode = ParsingHelpers.CleanNullOrEmptyValue(dataReader.ColumnValueList[0].ToString());
                    segment.SegmentId = (long)dataReader.ColumnValueList[1];
                    segment.SegmentName = ParsingHelpers.CleanNullOrEmptyValue(dataReader.ColumnValueList[2].ToString());
                    segmentList.Add(segment);
                }
            }


            if (segmentList != null && segmentList.Count > 0)
            {
                return segmentList;
            }

            return null;

        }

        public static List<object> GetSilverPOPDatabaseIdByCampaignID(long campaignID)
        {
            List<object> databseIdAndBlastId = new List<object>();
            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@campaignId", campaignID));
            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Get_SilverPOP_DatabaseId_By_Campaign_Id", spParamValue);

            if (dataList != null)
            {
                foreach (DataReaderObject dataReader in dataList)
                {
                    databseIdAndBlastId.Add(dataReader.ColumnValueList[0]);
                    databseIdAndBlastId.Add(dataReader.ColumnValueList[1]);
                    return databseIdAndBlastId;
                }
            }

            return null;
        }


        
        #region Get Active Campaign With Its Related Blast Information
        public static List<CampaignWithBlast> GetActiveCampaignWithItsBlast(long campaignId=default(long))
        {
            List<CampaignWithBlast> campaignWithItsBlast = new List<CampaignWithBlast>();

            if (campaignId == default(long) || campaignId <= 0)
            {
                GetActiveCampaingsWithBlast(campaignWithItsBlast);
            }
            else if(campaignId > 0)
            {
                GetActiveCampaingsWithBlastByCampaignId(campaignWithItsBlast,campaignId);
            }

            return (campaignWithItsBlast != null && campaignWithItsBlast.Count > 0) ? campaignWithItsBlast : null;

        }

        #region Methods For Getting Active Campaign By Campaign ID Or Get All Active Campaigns

        private static void GetActiveCampaingsWithBlastByCampaignId(List<CampaignWithBlast> campaignWithItsBlast,long campaignId)
        {

            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@CampaignId", campaignId));

            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Get_Active_Campaign_By_Campaign_Id",spParamValue);

            if (dataList != null)
            {
                foreach (DataReaderObject dataReader in dataList)
                {
                    CampaignWithBlast campaignBlast = new CampaignWithBlast();
                    campaignBlast.Campaign = new CampaignEntity(dataReader.ColumnValueList[1].ToString(), (long)dataReader.ColumnValueList[0]);
                    GetCurrentActiveBlastsByCampaignID(campaignBlast);
                    campaignWithItsBlast.Add(campaignBlast);
                }
            }
            else
            {
                throw new Exception("There is no active campaign found.");
            }
        }


        private static void GetActiveCampaingsWithBlast(List<CampaignWithBlast> campaignWithItsBlast)
        {

            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureAndGetDataObject("SPR_Get_Active_CampaignIDs");

            if (dataList != null)
            {
                foreach (DataReaderObject dataReader in dataList)
                {
                    CampaignWithBlast campaignBlast = new CampaignWithBlast();
                    campaignBlast.Campaign = new CampaignEntity(dataReader.ColumnValueList[1].ToString(), (long)dataReader.ColumnValueList[0]);
                    GetCurrentActiveBlastsByCampaignID(campaignBlast);
                    campaignWithItsBlast.Add(campaignBlast);
                }
            }
            else
            {
                throw new Exception("There is no active campaign found.");
            }
        }
        #endregion

        #region Get All Blasts By Campaign ID
        private static void GetCurrentActiveBlastsByCampaignID(CampaignWithBlast campaignBlast)
        {
            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@campaignId", campaignBlast.Campaign.CampaignID));
            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Get_Current_Active_Blast_Id", spParamValue);

            if (dataList != null)
            {
                foreach (DataReaderObject dataReader in dataList)
                {
                    campaignBlast.BlastIDs.Add((long)dataReader.ColumnValueList[0]);
                }
            }
        }
        #endregion

        public static DateTime GetLastDateForRawProspectTracking(long campaignId, long mailingId, string evenType)
        {
            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@CampaignId", campaignId));
            spParamValue.Add(new ParamWithValue("@MailingId", mailingId));
            spParamValue.Add(new ParamWithValue("@EventType", evenType));

            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Get_Last_Prospect_Tracking_Date", spParamValue);

            if (dataList != null)
            {
                DateTime dt = new DateTime();
                foreach (DataReaderObject dataReader in dataList)
                {
                    if (dataReader.ColumnValueList[0] != null)
                    {
                        dt = (DateTime)dataReader.ColumnValueList[0];
                    }
                }
                return dt;
            }
            else
            {
                return ConfigHelpers.TrackingStartTime;
            }
        }

        #endregion
    }
}
